package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC164 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 很强大很难很经典的题目，本解法使用了桶排序，非常经典的桶排序算法，收藏了
     * @return:
     */
    class Solution {
        public int maximumGap(int[] nums) {
            if (nums.length <= 1) {
                return 0;
            }
            List<ArrayList<Integer>> lists = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                lists.add(new ArrayList<>());
            }
            int n = nums.length;
            int max = nums[0];
            //找出最大的数字
            for (int i = 1; i < n; i++) {
                if (max < nums[i]) {
                    max = nums[i];
                }
            }
            int m = max;
            int exp = 1;
            //一位一位的进行
            while (max > 0) {
                //将之前的元素清空
                for (int i = 0; i < 10; i++) {
                    lists.set(i, new ArrayList<>());
                }
                //将数字放入对应的位置
                for (int i = 0; i < n; i++) {
                    lists.get(nums[i] / exp % 10).add(nums[i]);
                }

                //将数字依次拿出来
                int index = 0;
                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < lists.get(i).size(); j++) {
                        nums[index] = lists.get(i).get(j);
                        index++;
                    }

                }
                max /= 10;
                exp *= 10;
            }

            int maxGap = 0;
            for (int i = 0; i < nums.length - 1; i++) {
                if (nums[i + 1] - nums[i] > maxGap) {
                    maxGap = nums[i + 1] - nums[i];
                }
            }
            return maxGap;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC164 an = new LC164();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
